<script setup>
import { ref, onMounted } from "vue";
import { getLogList } from "@/api/log";
import { ElMessage } from "element-plus";
import { formatDateTime } from "@/utils/date";

// 日志列表数据
const logList = ref([]);

// 分页信息
const pagination = ref({
  currentPage: 1, // 当前页码
  pageSize: 10, // 每页显示数量
  total: "", // 总记录数
});

// 获取日志数据
const getLogs = async () => {
  const res = await getLogList(
    pagination.value.currentPage,
    pagination.value.pageSize
  );
  if (res.code) {
    logList.value = res.data.rows;
    pagination.value.total = res.data.total;
  } else {
    ElMessage.error(res.msg);
  }
};

// 根据耗时获取标签类型
const getCostTimeType = (time) => {
  if (time > 100) return "danger";
  if (time > 50) return "warning";
  return "success";
};

// 分页事件处理
const handleSizeChange = (val) => {
  pagination.value.pageSize = val;
  getLogs();
};

const handleCurrentChange = (val) => {
  pagination.value.currentPage = val;
  getLogs();
};

// 页面加载时获取日志数据
onMounted(() => {
  getLogs();
});
</script>

<template>
  <div class="log-container">
    <h1>操作日志</h1>
    <!-- 日志表格 -->
    <el-table :data="logList" border style="width: 100%" v-loading="loading">
      <el-table-column
        prop="operateEmpId"
        label="ID"
        width="80"
        align="center"
      />
      <el-table-column
        prop="operateEmpName"
        label="操作人"
        width="120"
        align="center"
      />
      <el-table-column
        prop="operateTime"
        label="操作时间"
        width="180"
        align="center"
      >
        <template #default="{ row }">
          {{ formatDateTime(row.operateTime) }}
        </template>
      </el-table-column>
      <el-table-column
        prop="className"
        label="类名"
        min-width="200"
        show-overflow-tooltip
      />
      <el-table-column
        prop="methodName"
        label="方法名"
        width="120"
        align="center"
      />
      <el-table-column
        prop="methodParams"
        label="方法参数"
        min-width="200"
        show-overflow-tooltip
      />
      <el-table-column
        prop="returnValue"
        label="返回值"
        min-width="200"
        show-overflow-tooltip
      />
      <el-table-column
        prop="costTime"
        label="耗时(ms)"
        width="100"
        align="center"
      >
        <template #default="{ row }">
          <el-tag :type="getCostTimeType(row.costTime)">
            {{ row.costTime }}
          </el-tag>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <el-pagination
      class="pagination"
      v-model:current-page="pagination.currentPage"
      v-model:page-size="pagination.pageSize"
      :total="pagination.total"
      :page-sizes="[10, 20, 50, 100]"
      layout="total, sizes, prev, pager, next, jumper"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
  </div>
</template>

<style scoped>
.log-container {
  padding: 20px;
}

h1 {
  margin-bottom: 20px;
  color: #333;
  font-size: 24px;
  font-family: "SimSun", sans-serif;
}

.pagination {
  margin-top: 20px;
  justify-content: flex-end;
}

/* 表格样式优化 */
:deep(.el-table) {
  margin-top: 20px;
}

:deep(.el-table__header th) {
  background-color: #f5f7fa;
  font-weight: bold;
}

/* 耗时标签样式 */
:deep(.el-tag) {
  margin: 2px;
}

/* 自定义 Tooltip 样式 */
:deep(.el-popper.is-dark) {
  background: aqua !important;
  border: none !important;
  color: black !important;
}
</style>
